<template>
  <div>
    {{ language }}
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="mini" style="width:120px" @change="dateChange">
        <el-option v-for="item in yearlist" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="currentMonth" size="mini" style="width:120px;margin-left:10px" @change="dateChange">
        <el-option v-for="item in 12" :key="item" :value="item" />
      </el-select>
    </el-row>
    <el-calendar v-if="isDatePickerVisible" ref="reset" v-model="currentTime">
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span :class="text"> {{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  filters: {
    getDay: (val) => {
      return new Date(val).getDate()
    //   const day = val.split('-')[2]
    //   return day.startsWith('0') ? day.slice(1) : day
    }
  },
  props: {
    startData: {
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      currentMonth: null,
      currentYear: null,
      currentDate: null,
      currentTime: null,
      yearlist: [],
      text: 'text',
      reset: true,
      isDatePickerVisible: true,
      i: 0
    }
  },
  computed: {
    ...mapGetters(['language'])
  },
  watch: {
    language() {
      this.isDatePickerVisible = false
      this.$nextTick(() => {
        this.isDatePickerVisible = true
      })
    }
  },
  created() {
    this.currentYear = this.startData.getFullYear()
    this.currentMonth = this.startData.getMonth() + 1
    this.currentDate = this.startData.getDate()
    this.yearlist = Array.from(Array(11), (v, i) => 2020 + i - 5)
    // this.currentTime = new Date(`${this.currentYear}-${this.currentMonth}-${this.currentDate}`)
    this.dateChange()
  },
  methods: {
    dateChange() {
      if (this.currentYear === new Date().getFullYear() && this.currentMonth === (new Date().getMonth() + 1)) {
        this.currentTime = new Date(`${this.currentYear}-${this.currentMonth}-${this.currentDate}`)
        this.text = 'text'
      } else {
        this.text = ''
        this.currentTime = new Date(`${this.currentYear}-${this.currentMonth}-1`)
      }
    },
    isWeek(date) {
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style  scoped>
 ::v-deep .el-calendar-day {
  height:  auto;
 }
 ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
</style>
